package o;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import java.util.concurrent.atomic.AtomicInteger;

public class ett {
    private static ett ˎ;
    private static long ˏ = 0;
    private static final Object ॱ = new Object();
    private AtomicInteger ʼ = new AtomicInteger();
    private awc ˊ;
    private final Runnable ˋ = new Runnable(this) {
        final /* synthetic */ ett ˏ;

        {
            this.ˏ = r1;
        }

        public void run() {
            if (ContentManager.getInstant().getContext() == null || System.currentTimeMillis() - ett.ˏ <= edl.ˏ().ˋᐝ() || this.ˏ.ʼ.get() <= 0) {
                this.ˏ.ॱॱ.removeCallbacks(this.ˏ.ˋ);
                this.ˏ.ॱॱ.postDelayed(this.ˏ.ˋ, edl.ˏ().ˋˋ());
                return;
            }
            ett.ˏ().ॱ("keepAlive", 3);
        }
    };
    private Handler ॱॱ = new Handler(this, Looper.getMainLooper()) {
        final /* synthetic */ ett ˎ;

        public void handleMessage(Message message) {
            super.handleMessage(message);
            if (message.what == 1 || message.what == 4) {
                this.ˎ.ʼ.incrementAndGet();
                this.ˎ.ˎ(message);
            } else if (message.what == 3) {
                this.ˎ.ˎ(message);
            } else if (message.what == 2 && this.ˎ.ʼ.decrementAndGet() <= 0 && this.ˎ.ᐝ) {
                this.ˎ.ॱ();
                this.ˎ.ᐝ = false;
                this.ˎ.ʼ = new AtomicInteger();
            }
        }
    };
    private boolean ᐝ = false;

    private ett() {
    }

    public static ett ˏ() {
        ett o_ett;
        synchronized (ॱ) {
            if (ˎ == null) {
                ˎ = new ett();
            }
            o_ett = ˎ;
        }
        return o_ett;
    }

    private static void ˋ(long j) {
        ˏ = j;
    }

    public void ॱ(String str) {
        ॱ(str, 1);
    }

    public void ॱ(final String str, final int i) {
        if (azc.ˎ().ॱ() < 17) {
            return;
        }
        if (this.ˊ == null) {
            try {
                this.ˊ = awc.ˏ(ContentManager.getInstant().getContext(), new awb(this) {
                    final /* synthetic */ ett ˋ;

                    public void ˏ() {
                        this.ˋ.ˊ = null;
                    }

                    public void ॱ() {
                        this.ˋ.ˊ(str, i);
                    }
                });
                return;
            } catch (Throwable e) {
                Logger.ˋ("PowerKitManager", "can not use HuaweiPowerKit SecurityException ", e);
                return;
            } catch (Throwable e2) {
                Logger.ˋ("PowerKitManager", "can not use HuaweiPowerKit Exception ", e2);
                return;
            }
        }
        ˊ(str, i);
    }

    private void ˊ(String str, int i) {
        Message obtain = Message.obtain(this.ॱॱ, i);
        obtain.obj = str;
        this.ॱॱ.sendMessage(obtain);
    }

    public void ˊ() {
        if (azc.ˎ().ॱ() >= 17) {
            this.ॱॱ.sendMessageDelayed(Message.obtain(this.ॱॱ, 2), 10000);
        }
    }

    private void ˎ(Message message) {
        CharSequence charSequence = "keepAlive";
        if (message.obj instanceof String) {
            charSequence = (String) message.obj;
        }
        if (this.ˊ != null) {
            String str;
            if (TextUtils.isEmpty(charSequence)) {
                str = "keepAlive";
            } else {
                CharSequence charSequence2 = charSequence;
            }
            try {
                if (message.what == 4 || message.what == 3) {
                    ˋ(System.currentTimeMillis());
                    this.ॱॱ.removeCallbacks(this.ˋ);
                    this.ॱॱ.postDelayed(this.ˋ, edl.ˏ().ˋˋ());
                }
                boolean ˏ = this.ˊ.ˏ(ContentManager.getInstant().getContext().getPackageName(), 65535, 1800000, str);
                if (ˏ) {
                    ˏ = this.ᐝ || ˏ;
                    this.ᐝ = ˏ;
                    Logger.ˎ("PowerKitManager", "apply resource:" + str);
                    return;
                }
                Logger.e("PowerKitManager", "apply failed");
            } catch (Throwable e) {
                Logger.ˋ("PowerKitManager", "applyForResourceUse failed", e);
                this.ˊ = null;
            } catch (Throwable e2) {
                Logger.ˋ("PowerKitManager", "applyForResourceUse failed", e2);
                this.ˊ = null;
            }
        }
    }

    private void ॱ() {
        if (this.ˊ != null) {
            try {
                if (this.ˊ.ॱ(ContentManager.getInstant().getContext().getPackageName(), 65535)) {
                    Logger.ˎ("PowerKitManager", "unapply resource");
                } else {
                    Logger.e("PowerKitManager", "unapply failed");
                }
            } catch (Throwable e) {
                Logger.ˋ("PowerKitManager", "unapplyForResourceUse FAILED", e);
                this.ˊ = null;
            } catch (Throwable e2) {
                Logger.ˋ("PowerKitManager", "unapplyForResourceUse FAILED", e2);
                this.ˊ = null;
            }
        }
    }
}
